﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Brain
{
    public class Camera2D : IBaseComponent, IDrawable, ICamera2D
    {
        public Vector3 GetPosition()
        {
            return Position3;
        }
        public void SetPosition(Vector3 value)
        {
            Position3 = value;
        }

        /// <summary>
        /// Sets the Position of the Camera. Z is for Zoom, X and Y makes a Vector2
        /// </summary>
        public Vector3 Position3 { get; set; }

        public bool ShouldDraw { get; set; }

        public string Name { get; set; }

        public BaseAnimationTimeLine AnimationTimeLine { get; set; }

        public Vector2 Position
        {
            get { return new Vector2(Position3.X, Position3.Y); }
            set
            {
                Position3 = new Vector3(value.X, value.Y, Position3.Z);
            }
        }

        public float Zoom
        {
            get { return Position3.Z; }
            set
            {
                Position3 = new Vector3(Position3.X, Position3.Y, value);
            }
        }

        /// <summary>
        /// World Matrix
        /// </summary>
        public Matrix World { get; set; }

        /// <summary>
        /// Camera Rotation in Degrees. This uses MathHelper to convert to Radians.
        /// </summary>
        public float DegreesRotation
        {
            get { return MathHelper.ToDegrees(RotationRadians); }
            set
            {
                this.RotationRadians = MathHelper.ToRadians(value);
            }
        }

        /// <summary>
        /// Camera Rotation in Radians
        /// </summary>
        public float RotationRadians { get; set; }

        public bool ShouldSerialize { get; set; }
        /// <summary>
        /// Instantiates a new Camera 2D
        /// </summary>
        /// <param name="GameScreen"></param>
        public Camera2D(GameScreen gs)
        {
            gs.AddComponent(this);
            Position3 = new Vector3(0, 0, 1);

            ShouldDraw = true;

            Name = NameUtil.GetUniqueName("Camera2D");

            AnimationTimeLine = new BaseAnimationTimeLine();
            ShouldSerialize = true;

            World = Matrix.Identity;
        }

        /// <summary>
        /// Instantiates a new Camera 2D
        /// </summary>
        public Camera2D()
        {
            Position3 = new Vector3(0, 0, 1);

            ShouldDraw = true;

            Name = NameUtil.GetUniqueName("Camera2D");

            AnimationTimeLine = new BaseAnimationTimeLine();
        }

        /// <summary>
        /// Calculates the World matrix
        /// </summary>
        public virtual void Draw()
        {
            AnimationTimeLine.Update();
            World = MatrixUtil.CreateWorldMatrix(Matrix.CreateScale(Position3.Z),
                Matrix.CreateRotationZ(RotationRadians),
                Matrix.CreateTranslation(new Vector3(Position3.X, Position3.Y, 0)));
        }

        public void SerializeData(SerializationData parentData, string index)
        {
            parentData.AddData("Camera2D.Name", this.Name, index);
            parentData.AddData("Camera2D.Position3", this.Position3, index);
            parentData.AddData("Camera2D.RotationRadians", this.RotationRadians, index);
            parentData.AddData("Camera2D.ShouldDraw", this.ShouldDraw, index);
        }

        public void DeserializeData(SerializationData parentData, string index)
        {
            this.Name = parentData.GetData<string>("Camera2D.Name", index);
            this.Position3 = parentData.GetData<Vector3>("Camera2D.Position3", index);
            this.RotationRadians = parentData.GetData<float>("Camera2D.RotationRadians", index);
            this.ShouldDraw = parentData.GetData<bool>("Camera2D.ShouldDraw", index);
        }



        public void DeserializedAll()
        {
        }
    }
}
